using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using IGlobalist.Cms.Utils;

namespace IGlobalist.Cms.Model
{
    [Serializable]
    public class PageList : IXml, IList<IPage>
    {

        private IList<IPage> _PageList = new List<IPage>();
        #region IList<IPage> Members

        public int IndexOf(IPage item)
        {
            return _PageList.IndexOf(item);
        }

        public void Insert(int index, IPage item)
        {
            _PageList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _PageList.RemoveAt(index);
        }

        public IPage this[int index]
        {
            get
            {
                return _PageList[index];
            }
            set
            {
                _PageList[index] = value;
            }
        }

        #endregion

        #region IList<IPage> Members

        public void Add(IPage item)
        {
            _PageList.Add(item);
        }

        public void Clear()
        {
            _PageList.Clear();
        }

        public bool Contains(IPage item)
        {
            return _PageList.Contains(item);
        }

        public void CopyTo(IPage[] array, int arrayIndex)
        {
            _PageList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _PageList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _PageList.IsReadOnly; }
        }

        public bool Remove(IPage item)
        {
            return _PageList.Remove(item);
        }

        #endregion

        #region IEnumerable<IPage> Members

        public IEnumerator<IPage> GetEnumerator()
        {
            return _PageList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//page");
                _PageList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IPage page = (IPage) CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Page);
                    if (page.FromXml(node.InnerXml))
                        _PageList.Add(page);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<pages>");
            foreach (IPage page in _PageList)
            {
                sb.Append(page.ToXmlString());
            }
            sb.Append("</pages>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_PageList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ContentCategoryList : IXml, IList<IContentCategory>
    {

        private IList<IContentCategory> _ContentCategoryList = new List<IContentCategory>();
        #region IList<IContentCategory> Members

        public int IndexOf(IContentCategory item)
        {
            return _ContentCategoryList.IndexOf(item);
        }

        public void Insert(int index, IContentCategory item)
        {
            _ContentCategoryList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ContentCategoryList.RemoveAt(index);
        }

        public IContentCategory this[int index]
        {
            get
            {
                return _ContentCategoryList[index];
            }
            set
            {
                _ContentCategoryList[index] = value;
            }
        }

        #endregion

        #region IList<IContentCategory> Members

        public void Add(IContentCategory item)
        {
            _ContentCategoryList.Add(item);
        }

        public void Clear()
        {
            _ContentCategoryList.Clear();
        }

        public bool Contains(IContentCategory item)
        {
            return _ContentCategoryList.Contains(item);
        }

        public void CopyTo(IContentCategory[] array, int arrayIndex)
        {
            _ContentCategoryList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ContentCategoryList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ContentCategoryList.IsReadOnly; }
        }

        public bool Remove(IContentCategory item)
        {
            return _ContentCategoryList.Remove(item);
        }

        #endregion

        #region IEnumerable<IContentCategory> Members

        public IEnumerator<IContentCategory> GetEnumerator()
        {
            return _ContentCategoryList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//contentCategory");
                _ContentCategoryList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IContentCategory contentCategory = (IContentCategory)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.ContentCategory);
                    if (contentCategory.FromXml(node.InnerXml))
                        _ContentCategoryList.Add(contentCategory);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<categories>");
            foreach (IContentCategory contentCategory in _ContentCategoryList)
            {
                sb.Append(contentCategory.ToXmlString());
            }
            sb.Append("</categories>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_ContentCategoryList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ContentList : IXml, IList<IContent>
    {

        private IList<IContent> _ContentList = new List<IContent>();
        #region IList<IContent> Members

        public int IndexOf(IContent item)
        {
            return _ContentList.IndexOf(item);
        }

        public void Insert(int index, IContent item)
        {
            _ContentList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ContentList.RemoveAt(index);
        }

        public IContent this[int index]
        {
            get
            {
                return _ContentList[index];
            }
            set
            {
                _ContentList[index] = value;
            }
        }

        #endregion

        #region IList<IContent> Members

        public void Add(IContent item)
        {
            _ContentList.Add(item);
        }

        public void Clear()
        {
            _ContentList.Clear();
        }

        public bool Contains(IContent item)
        {
            return _ContentList.Contains(item);
        }

        public void CopyTo(IContent[] array, int arrayIndex)
        {
            _ContentList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ContentList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ContentList.IsReadOnly; }
        }

        public bool Remove(IContent item)
        {
            return _ContentList.Remove(item);
        }

        #endregion

        #region IEnumerable<IContent> Members

        public IEnumerator<IContent> GetEnumerator()
        {
            return _ContentList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//content");
                _ContentList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IContent content = (IContent)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Content);
                    if (content.FromXml(node.InnerXml))
                        _ContentList.Add(content);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<contents>");
            foreach (IContent content in _ContentList)
            {
                sb.Append(content.ToXmlString());
            }
            sb.Append("</contents>");
            return sb.ToString();
        }

        #endregion

  
        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_ContentList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ProductList : IXml, IList<IProduct>
    {

        private IList<IProduct> _ProductList = new List<IProduct>();
        #region IList<IProduct> Members

        public int IndexOf(IProduct item)
        {
            return _ProductList.IndexOf(item);
        }

        public void Insert(int index, IProduct item)
        {
            _ProductList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ProductList.RemoveAt(index);
        }

        public IProduct this[int index]
        {
            get
            {
                return _ProductList[index];
            }
            set
            {
                _ProductList[index] = value;
            }
        }

        #endregion

        #region IList<IContent> Members

        public void Add(IProduct item)
        {
            _ProductList.Add(item);
        }

        public void Clear()
        {
            _ProductList.Clear();
        }

        public bool Contains(IProduct item)
        {
            return _ProductList.Contains(item);
        }

        public void CopyTo(IProduct[] array, int arrayIndex)
        {
            _ProductList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ProductList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ProductList.IsReadOnly; }
        }

        public bool Remove(IProduct item)
        {
            return _ProductList.Remove(item);
        }

        #endregion

        #region IEnumerable<IProduct> Members

        public IEnumerator<IProduct> GetEnumerator()
        {
            return _ProductList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//content");
                _ProductList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IProduct product = (IProduct)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Content);
                    if (product.FromXml(node.InnerXml))
                        _ProductList.Add(product);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<contents>");
            foreach (IProduct product in _ProductList)
            {
                sb.Append(product.ToXmlString());
            }
            sb.Append("</contents>");
            return sb.ToString();
        }

        #endregion


        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_ProductList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class PropertyList : IXml, IList<IContentProperty>
    {
        private IList<IContentProperty> _PropertyList = new List<IContentProperty>();
   

        public IContentProperty GetProperty(string name)
        {
            foreach (IContentProperty item in _PropertyList)
            {
                if (item.Name.Equals(name))
                {
                    return item;
                }
            }
            return null;
        }
        public void SetPropertyValue(string name, object value){
            IContentProperty p = GetProperty(name);
            if (p == null)
                throw new Exception("Property [" + name + "] not found.");
            p.Value = value;
        }

        public object GetPropertyValue(string name)
        {
            IContentProperty p = GetProperty(name);
            if (p == null)
                throw new Exception("Property [" + name + "] not found.");
            return p.Value ;
        }

        #region IList<IContentProperty> Members

        public int IndexOf(IContentProperty item)
        {
            return _PropertyList.IndexOf(item);
        }

        public void Insert(int index, IContentProperty item)
        {
            if (_PropertyList.IndexOf(item) < 0)
            {
                _PropertyList.Insert(index, item);
            }
        }

        public void RemoveAt(int index)
        {
            _PropertyList.RemoveAt(index);
        }

        public IContentProperty this[int index]
        {
            get
            {
                return _PropertyList[index];
            }
            set
            {
                if (_PropertyList.IndexOf(value) < 0)
                {
                    _PropertyList[index] = value;
                }
            }
        }

        #endregion

        #region IList<IContentProperty> Members

        public void Add(IContentProperty item)
        {
            if (_PropertyList.IndexOf(item) < 0)
            {
                _PropertyList.Add(item);
            }
        }

        public void Clear()
        {
            _PropertyList.Clear();
        }

        public bool Contains(IContentProperty item)
        {
            return _PropertyList.Contains(item);
        }

        public void CopyTo(IContentProperty[] array, int arrayIndex)
        {
            _PropertyList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _PropertyList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _PropertyList.IsReadOnly; }
        }

        public bool Remove(IContentProperty item)
        {
            return _PropertyList.Remove(item);
        }
        public bool Remove(string itemName)
        {
            foreach (IContentProperty item in _PropertyList)
            {
                if (item.Name.Equals(itemName))
                {
                    _PropertyList.Remove(item);
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region IEnumerable<IContentProperty> Members

        public IEnumerator<IContentProperty> GetEnumerator()
        {
            return _PropertyList.GetEnumerator();
        }

        #endregion



        #region IXml Members

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<properties>");
            foreach (IContentProperty property in _PropertyList)
            {
                sb.Append(property.ToXmlString());
            }
            sb.Append("</properties>");
            return sb.ToString();
        }
        public bool FromXml(string xml)
        {
            if (string.IsNullOrEmpty(xml))
            {
                _PropertyList.Clear();
                return true;
            }
            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//property");
                _PropertyList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IContentProperty property = (IContentProperty)new BaseProperty();
                    if (property.FromXml(node.OuterXml))
                        _PropertyList.Add(property);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator) _PropertyList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ResourceFileList : IXml, IList<IResource>
    {

        private IList<IResource> _ResourceFileList = new List<IResource>();
        #region IList<IResource> Members

        public int IndexOf(IResource item)
        {
            return _ResourceFileList.IndexOf(item);
        }

        public void Insert(int index, IResource item)
        {
            _ResourceFileList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ResourceFileList.RemoveAt(index);
        }

        public IResource this[int index]
        {
            get
            {
                return _ResourceFileList[index];
            }
            set
            {
                _ResourceFileList[index] = value;
            }
        }

        #endregion

        #region IList<IResource> Members

        public void Add(IResource item)
        {
            _ResourceFileList.Add(item);
        }

        public void Clear()
        {
            _ResourceFileList.Clear();
        }

        public bool Contains(IResource item)
        {
            return _ResourceFileList.Contains(item);
        }

        public void CopyTo(IResource[] array, int arrayIndex)
        {
            _ResourceFileList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ResourceFileList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ResourceFileList.IsReadOnly; }
        }

        public bool Remove(IResource item)
        {
            return _ResourceFileList.Remove(item);
        }

        #endregion

        #region IEnumerable<IResource> Members

        public IEnumerator<IResource> GetEnumerator()
        {
            return _ResourceFileList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//resource");
                _ResourceFileList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IResource resource = (IResource)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.ResourceFile);
                    if (resource.FromXml(node.InnerXml))
                        _ResourceFileList.Add(resource);
                }
                return true;
            }
            catch 
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<resources>");
            foreach (IResource resource in _ResourceFileList)
            {
                sb.Append(resource.ToXmlString());
            }
            sb.Append("</resources>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator) _ResourceFileList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ResourceFolderList : IXml, IList<IResourceFolder>
    {

        private IList<IResourceFolder> _ResourceFolderList = new List<IResourceFolder>();
        #region IList<IResourceFolder> Members

        public int IndexOf(IResourceFolder item)
        {
            return _ResourceFolderList.IndexOf(item);
        }

        public void Insert(int index, IResourceFolder item)
        {
            _ResourceFolderList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ResourceFolderList.RemoveAt(index);
        }

        public IResourceFolder this[int index]
        {
            get
            {
                return _ResourceFolderList[index];
            }
            set
            {
                _ResourceFolderList[index] = value;
            }
        }

        #endregion

        #region IList<IResourceFolder> Members

        public void Add(IResourceFolder item)
        {
            _ResourceFolderList.Add(item);
        }

        public void Clear()
        {
            _ResourceFolderList.Clear();
        }

        public bool Contains(IResourceFolder item)
        {
            return _ResourceFolderList.Contains(item);
        }

        public void CopyTo(IResourceFolder[] array, int arrayIndex)
        {
            _ResourceFolderList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ResourceFolderList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ResourceFolderList.IsReadOnly; }
        }

        public bool Remove(IResourceFolder item)
        {
            return _ResourceFolderList.Remove(item);
        }

        #endregion

        #region IEnumerable<IResourceFolder> Members

        public IEnumerator<IResourceFolder> GetEnumerator()
        {
            return _ResourceFolderList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//resourceFolderList");
                _ResourceFolderList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IResourceFolder resourceFolderList = (IResourceFolder)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.ResourceFolder);
                    if (resourceFolderList.FromXml(node.InnerXml))
                        _ResourceFolderList.Add(resourceFolderList);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<resourceFolderLists>");
            foreach (IResourceFolder resourceFolderList in _ResourceFolderList)
            {
                sb.Append(resourceFolderList.ToXmlString());
            }
            sb.Append("</resourceFolderLists>");
            return sb.ToString();
        }

        #endregion


        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_ResourceFolderList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class OrderLineList : IXml, IList<IOrderLine>
    {

        private IList<IOrderLine> _OrderLineList = new List<IOrderLine>();
        #region IList<IOrderLine> Members

        public int IndexOf(IOrderLine item)
        {
            return _OrderLineList.IndexOf(item);
        }

        public void Insert(int index, IOrderLine item)
        {
            _OrderLineList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _OrderLineList.RemoveAt(index);
        }

        public IOrderLine this[int index]
        {
            get
            {
                return _OrderLineList[index];
            }
            set
            {
                _OrderLineList[index] = value;
            }
        }

        #endregion

        #region IList<IOrderLine> Members

        public void Add(IOrderLine item)
        {
            _OrderLineList.Add(item);
        }

        public void Clear()
        {
            _OrderLineList.Clear();
        }

        public bool Contains(IOrderLine item)
        {
            return _OrderLineList.Contains(item);
        }

        public void CopyTo(IOrderLine[] array, int arrayIndex)
        {
            _OrderLineList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _OrderLineList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _OrderLineList.IsReadOnly; }
        }

        public bool Remove(IOrderLine item)
        {
            return _OrderLineList.Remove(item);
        }

        #endregion

        #region IEnumerable<IOrderLine> Members

        public IEnumerator<IOrderLine> GetEnumerator()
        {
            return _OrderLineList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//resourceFolderList");
                _OrderLineList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IOrderLine orderLineList = (IOrderLine)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.OrderLine);
                    if (orderLineList.FromXml(node.InnerXml))
                        _OrderLineList.Add(orderLineList);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<orderLineList>");
            foreach (IOrderLine orderLineList in _OrderLineList)
            {
                sb.Append(orderLineList.ToXmlString());
            }
            sb.Append("</orderLineList>");
            return sb.ToString();
        }

        #endregion


        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_OrderLineList.GetEnumerator();
        }

        #endregion
    }
    

    [Serializable]
    public class TemplateList : IXml, IList<ITemplate>
    {

        private IList<ITemplate> _TemplateList = new List<ITemplate>();
        #region IList<ITemplate> Members

        public int IndexOf(ITemplate item)
        {
            return _TemplateList.IndexOf(item);
        }

        public void Insert(int index, ITemplate item)
        {
            _TemplateList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _TemplateList.RemoveAt(index);
        }

        public ITemplate this[int index]
        {
            get
            {
                return _TemplateList[index];
            }
            set
            {
                _TemplateList[index] = value;
            }
        }

        #endregion

        #region IList<ITemplate> Members

        public void Add(ITemplate item)
        {
            _TemplateList.Add(item);
        }

        public void Clear()
        {
            _TemplateList.Clear();
        }

        public bool Contains(ITemplate item)
        {
            return _TemplateList.Contains(item);
        }

        public void CopyTo(ITemplate[] array, int arrayIndex)
        {
            _TemplateList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _TemplateList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _TemplateList.IsReadOnly; }
        }

        public bool Remove(ITemplate item)
        {
            return _TemplateList.Remove(item);
        }

        #endregion

        #region IEnumerable<ITemplate> Members

        public IEnumerator<ITemplate> GetEnumerator()
        {
            return _TemplateList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//template");
                _TemplateList.Clear();
                foreach (XmlNode node in nodes)
                {
                    ITemplate template = (ITemplate)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Template);
                    if (template.FromXml(node.InnerXml))
                        _TemplateList.Add(template);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<templates>");
            foreach (ITemplate template in _TemplateList)
            {
                sb.Append(template.ToXmlString());
            }
            sb.Append("</templates>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_TemplateList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class ContentNoteList : IXml, IList<IContentNote>
    {

        private IList<IContentNote> _ContentNoteList = new List<IContentNote>();
        #region IList<IContentNote> Members

        public int IndexOf(IContentNote item)
        {
            return _ContentNoteList.IndexOf(item);
        }

        public void Insert(int index, IContentNote item)
        {
            _ContentNoteList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _ContentNoteList.RemoveAt(index);
        }

        public IContentNote this[int index]
        {
            get
            {
                return _ContentNoteList[index];
            }
            set
            {
                _ContentNoteList[index] = value;
            }
        }

        #endregion

        #region IList<IContentNote> Members

        public void Add(IContentNote item)
        {
            _ContentNoteList.Add(item);
        }

        public void Clear()
        {
            _ContentNoteList.Clear();
        }

        public bool Contains(IContentNote item)
        {
            return _ContentNoteList.Contains(item);
        }

        public void CopyTo(IContentNote[] array, int arrayIndex)
        {
            _ContentNoteList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _ContentNoteList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _ContentNoteList.IsReadOnly; }
        }

        public bool Remove(IContentNote item)
        {
            return _ContentNoteList.Remove(item);
        }

        #endregion

        #region IEnumerable<IContentNote> Members

        public IEnumerator<IContentNote> GetEnumerator()
        {
            return _ContentNoteList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//contentNote");
                _ContentNoteList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IContentNote contentNote = (IContentNote)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.ContentNote);
                    if (contentNote.FromXml(node.InnerXml))
                        _ContentNoteList.Add(contentNote);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<contentNotes>");
            foreach (IContentNote contentNote in _ContentNoteList)
            {
                sb.Append(contentNote.ToXmlString());
            }
            sb.Append("</contentNotes>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_ContentNoteList.GetEnumerator(); 
        }

        #endregion
    }

    [Serializable]
    public class RoleList : IXml, IList<IRole>
    {

        private IList<IRole> _RoleList = new List<IRole>();
        #region IList<IRole> Members

        public int IndexOf(IRole item)
        {
            return _RoleList.IndexOf(item);
        }

        public void Insert(int index, IRole item)
        {
            _RoleList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _RoleList.RemoveAt(index);
        }

        public IRole this[int index]
        {
            get
            {
                return _RoleList[index];
            }
            set
            {
                _RoleList[index] = value;
            }
        }

        #endregion

        #region IList<IRole> Members

        public void Add(IRole item)
        {
            _RoleList.Add(item);
        }

        public void Clear()
        {
            _RoleList.Clear();
        }

        public bool Contains(IRole item)
        {
            return _RoleList.Contains(item);
        }

        public void CopyTo(IRole[] array, int arrayIndex)
        {
            _RoleList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _RoleList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _RoleList.IsReadOnly; }
        }

        public bool Remove(IRole item)
        {
            return _RoleList.Remove(item);
        }

        #endregion

        #region IEnumerable<IRole> Members

        public IEnumerator<IRole> GetEnumerator()
        {
            return _RoleList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//role");
                _RoleList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IRole role = (IRole)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Role);
                    if (role.FromXml(node.InnerXml))
                        _RoleList.Add(role);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<roles>");
            foreach (IRole role in _RoleList)
            {
                sb.Append(role.ToXmlString());
            }
            sb.Append("</roles>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_RoleList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class UserList : IXml, IList<IUser>
    {

        private IList<IUser> _UserList = new List<IUser>();
        #region IList<IUser> Members

        public int IndexOf(IUser item)
        {
            return _UserList.IndexOf(item);
        }

        public void Insert(int index, IUser item)
        {
            _UserList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _UserList.RemoveAt(index);
        }

        public IUser this[int index]
        {
            get
            {
                return _UserList[index];
            }
            set
            {
                _UserList[index] = value;
            }
        }

        #endregion

        #region IList<IUser> Members

        public void Add(IUser item)
        {
            _UserList.Add(item);
        }

        public void Clear()
        {
            _UserList.Clear();
        }

        public bool Contains(IUser item)
        {
            return _UserList.Contains(item);
        }

        public void CopyTo(IUser[] array, int arrayIndex)
        {
            _UserList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _UserList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _UserList.IsReadOnly; }
        }

        public bool Remove(IUser item)
        {
            return _UserList.Remove(item);
        }

        #endregion

        #region IEnumerable<IUser> Members

        public IEnumerator<IUser> GetEnumerator()
        {
            return _UserList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//user");
                _UserList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IUser user = (IUser)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.User);
                    if (user.FromXml(node.InnerXml))
                        _UserList.Add(user);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<users>");
            foreach (IUser user in _UserList)
            {
                sb.Append(user.ToXmlString());
            }
            sb.Append("</users>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_UserList.GetEnumerator();
        }

        #endregion
    }
    [Serializable]
    public class AdContentList : IXml, IList<IAdContent>
    {

        private IList<IAdContent> _AdContentList = new List<IAdContent>();
        #region IList<IAdContent> Members

        public int IndexOf(IAdContent item)
        {
            return _AdContentList.IndexOf(item);
        }

        public void Insert(int index, IAdContent item)
        {
            _AdContentList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _AdContentList.RemoveAt(index);
        }

        public IAdContent this[int index]
        {
            get
            {
                return _AdContentList[index];
            }
            set
            {
                _AdContentList[index] = value;
            }
        }

        #endregion

        #region IList<IAdContent> Members

        public void Add(IAdContent item)
        {
            _AdContentList.Add(item);
        }

        public void Clear()
        {
            _AdContentList.Clear();
        }

        public bool Contains(IAdContent item)
        {
            return _AdContentList.Contains(item);
        }

        public void CopyTo(IAdContent[] array, int arrayIndex)
        {
            _AdContentList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _AdContentList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _AdContentList.IsReadOnly; }
        }

        public bool Remove(IAdContent item)
        {
            return _AdContentList.Remove(item);
        }

        #endregion

        #region IEnumerable<IAdContent> Members

        public IEnumerator<IAdContent> GetEnumerator()
        {
            return _AdContentList.GetEnumerator();
        }

        #endregion


        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//adContent");
                _AdContentList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IAdContent adContent = (IAdContent)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.AdContent);
                    if (adContent.FromXml(node.InnerXml))
                        _AdContentList.Add(adContent);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<adContents>");
            foreach (IAdContent adContent in _AdContentList)
            {
                sb.Append(adContent.ToXmlString());
            }
            sb.Append("</adContents>");
            return sb.ToString();
        }

        #endregion


        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_AdContentList.GetEnumerator();
        }

        #endregion
    }

    [Serializable]
    public class VideoList : IXml, IList<IVideo>
    {
        private IList<IVideo> _VideoList = new List<IVideo>();
        #region IList<IVideo> Members

        public int IndexOf(IVideo item)
        {
            return _VideoList.IndexOf(item);
        }

        public void Insert(int index, IVideo item)
        {
            _VideoList.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            _VideoList.RemoveAt(index);
        }

        public IVideo this[int index]
        {
            get
            {
                return _VideoList[index];
            }
            set
            {
                _VideoList[index] = value;
            }
        }

        #endregion

        #region IList<IVideo> Members

        public void Add(IVideo item)
        {
            _VideoList.Add(item);
        }

        public void Clear()
        {
            _VideoList.Clear();
        }

        public bool Contains(IVideo item)
        {
            return _VideoList.Contains(item);
        }

        public void CopyTo(IVideo[] array, int arrayIndex)
        {
            _VideoList.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return _VideoList.Count; }
        }

        public bool IsReadOnly
        {
            get { return _VideoList.IsReadOnly; }
        }

        public bool Remove(IVideo item)
        {
            return _VideoList.Remove(item);
        }

        #endregion

        #region IEnumerable<IVideo> Members

        public IEnumerator<IVideo> GetEnumerator()
        {
            return _VideoList.GetEnumerator();
        }

        #endregion

        #region IXml Members

        public bool FromXml(string xml)
        {

            try
            {
                XmlDocument xdoc = new XmlDocument();
                xdoc.InnerXml = xml;
                XmlNodeList nodes = xdoc.SelectNodes("//video");
                _VideoList.Clear();
                foreach (XmlNode node in nodes)
                {
                    IVideo video = (IVideo)CmsObjectFactory.GetInstance()
                                    .GetNewCmsObject(CmsObjectType.Video);
                    if (video.FromXml(node.InnerXml))
                        _VideoList.Add(video);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public string ToXmlString()
        {
            System.Text.StringBuilder sb = new StringBuilder();
            sb.Append("<videos>");
            foreach (IVideo video in _VideoList)
            {
                sb.Append(video.ToXmlString());
            }
            sb.Append("</videos>");
            return sb.ToString();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (System.Collections.IEnumerator)_VideoList.GetEnumerator();
        }

        #endregion

    }

}
